package cn.seapon.talker.common;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 只用用户有消息内容，可以是文本，图片,这里只做文本处理
 * @author Asuron
 * Created on 2014年12月28日 下午8:47:47
 */
public class MessageContent{
	//聊天消息实体
	private ChatMsgEntity chatMsgEntity = new ChatMsgEntity();
	//请求登录体
	private LoginRequestEntity loginEntity = new LoginRequestEntity();
	//登录请求返回结果
	private boolean resultCode = false; 
	//登录请求返回的用户信息
	private UserInformation userInfo = new UserInformation();
	//登录时返回所有好友和好友在线情况
	private FriendListInfomation friendInfos = new FriendListInfomation();
	//登录时返回的好友分组信息
	private GroupInfo groupInfo = new GroupInfo();
	//未读取的消息列表
	private ChatMsgEntity[] unreads = new ChatMsgEntity[0];
	//其他消息以字符串表示
	private String others = "";
	private FriendRequestEntity[] friendRequests = new FriendRequestEntity[0];

	
	public FriendRequestEntity getSingleFriendRequestEntity(){
		return friendRequests[0];
	}
	public void setSingleFriendRequestEntity(FriendRequestEntity entity){
		friendRequests = new FriendRequestEntity[1];
		friendRequests[0] = entity;
	}
	public FriendRequestEntity[] getAllFriendRequestEntities() {
		return friendRequests;
	}
	
	public void setFriendRequest(FriendRequestEntity[] friendRequest) {
		this.friendRequests = friendRequest;
	}


	public JSONObject getJsonObject(){
		JSONObject jsonObject = new JSONObject();
		
		JSONObject chatJsonObject = chatMsgEntity.getJsonObject();
		JSONObject loginEntityObject = loginEntity.getJSONObject();
		JSONObject userInfObject = userInfo.getJSONObject();
		JSONArray friendArray = friendInfos.getJsonArray();
		JSONArray groupInfos = groupInfo.getJsonArray();
		JSONArray unreadArray = new JSONArray();
		for (int i = 0; i < unreads.length; i++) {
			unreadArray.put(unreads[i].getJsonObject());
		}
		JSONArray fr = new JSONArray();
		for (int i = 0; i < friendRequests.length; i++) {
			fr.put(friendRequests[i].getJsonObject());
		}
		try {
			jsonObject.put("chatMsgEntity", chatJsonObject);
			jsonObject.put("loginEntity", loginEntityObject);
			jsonObject.put("resultCode", resultCode);
			jsonObject.put("userInfo", userInfObject);
			jsonObject.put("friendInfos", friendArray);
			jsonObject.put("groupInfo", groupInfos);
			jsonObject.put("unreads", unreadArray);
			jsonObject.put("others", others);
			jsonObject.put("friendRequests", fr);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return jsonObject;
	}
	
	public static MessageContent getObjectFromJson(JSONObject jsonObject){
		MessageContent content = new MessageContent();
		try {
			ChatMsgEntity chatMsgEntity = ChatMsgEntity.getObjectFromJson(jsonObject.getJSONObject("chatMsgEntity"));
			LoginRequestEntity loginEntity = LoginRequestEntity.getObjectFromJson(jsonObject.getJSONObject("loginEntity"));
			boolean resultCode = jsonObject.getBoolean("resultCode"); 
			UserInformation userInfo = UserInformation.getObjectFromJson(jsonObject.getJSONObject("userInfo"));
			FriendListInfomation friendInfos = FriendListInfomation.getObjectFromJson(jsonObject.getJSONArray("friendInfos"));
			GroupInfo groupInfo = GroupInfo.getObjectFromJson(jsonObject.getJSONArray("groupInfo"));
			
			JSONArray unreads = jsonObject.getJSONArray("unreads");
			ChatMsgEntity[] unreadsArray = new ChatMsgEntity[unreads.length()];
			for (int i = 0; i < unreads.length(); i++) {
				unreadsArray[i] = ChatMsgEntity.getObjectFromJson(unreads.getJSONObject(i));
			}
			String others = jsonObject.getString("others");
			
			JSONArray friendRequestArray  = jsonObject.getJSONArray("friendRequests");
			FriendRequestEntity[] friendRequestEntities = new FriendRequestEntity[friendRequestArray.length()];
			for (int i = 0; i < friendRequestEntities.length; i++) {
				friendRequestEntities[i] = FriendRequestEntity.getObjectFromJson(friendRequestArray.getJSONObject(i));
			}
			
			content.setChatMsgEntity(chatMsgEntity);
			content.setFriendInfos(friendInfos);
			content.setLoginEntity(loginEntity);
			content.setResultCode(resultCode);
			content.setUserInfo(userInfo);
			content.setGroupInfo(groupInfo);
			content.setUnreads(unreadsArray);
			content.setOthers(others);
			content.setFriendRequest(friendRequestEntities);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return content;
	}
	

	//------------  Constructor ---------------//
	public MessageContent() {
	}
	
	//------------  Getter and Setter ---------------//
	public ChatMsgEntity getChatMsgEntity() {
		return chatMsgEntity;
	}
	public void setChatMsgEntity(ChatMsgEntity chatMsgEntity) {
		this.chatMsgEntity = chatMsgEntity;
	}
	public boolean getResultCode() {
		return resultCode;
	}

	public void setResultCode(boolean resultCode) {
		this.resultCode = resultCode;
	}


	public LoginRequestEntity getLoginEntity() {
		return loginEntity;
	}

	public void setLoginEntity(LoginRequestEntity loginEntity) {
		this.loginEntity = loginEntity;
	}

	public UserInformation getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInformation userInfo) {
		this.userInfo = userInfo;
	}

	public FriendListInfomation getFriendInfos() {
		return friendInfos;
	}

	public void setFriendInfos(FriendListInfomation friendInfos) {
		this.friendInfos = friendInfos;
	}
	
	public GroupInfo getGroupInfo() {
		return groupInfo;
	}

	public void setGroupInfo(GroupInfo groupInfo) {
		this.groupInfo = groupInfo;
	}
	public ChatMsgEntity[] getUnreads() {
		return unreads;
	}

	public void setUnreads(ChatMsgEntity[] unreads) {
		this.unreads = unreads;
	}
	public String getOthers() {
		return others;
	}

	public void setOthers(String others) {
		this.others = others;
	}
}
